package com.github.chirspan.xaas.cfg.web.rest;


import com.github.chirspan.xaas.cfg.model.CfgDict;
import com.github.chirspan.xaas.api.BaseCRUDRestController;
import com.github.chirspan.xaas.api.BaseCRUDWrapper;
import com.github.chirspan.xaas.cfg.service.ICfgDictService;
import com.github.chirspan.xaas.core.exception.BizException;
import com.github.chirspan.xaas.core.rest.RestResult;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Authorization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.github.chirspan.xaas.cfg.model.CfgDictItem;

import java.util.List;


/**
 * @Author: chenpan
 * @Date: 2021-04-07 18:24:53
 * @Description: 数据字典内容 REST API
 */
@RestController
@RequestMapping("cfgDictItem")
public class CfgDictItemController extends BaseCRUDRestController<CfgDictItem> {

    @Autowired
    private ICfgDictService dictService;

    @GetMapping("/content")
    @ApiOperation(value = "查询字典内容", authorizations = {@Authorization(value = "apiKey")})
    public RestResult content(@RequestParam("code") String code) throws IllegalAccessException {
        CfgDict dict = dictService.getDict(code);
        if (dict == null) {
            throw new BizException("字典不存在");
        }
        CfgDictItem cre = new CfgDictItem();
        cre.setDictId(dict.getId());
        cre.setSortBy("value");
        List<CfgDictItem> rs = baseService.selectByWrapper(BaseCRUDWrapper.query(cre));
        return RestResult.OK(rs);
    }
}